package com.cskaoyan.listener;

import com.cskaoyan.service.AdminServiceImpl;
import com.cskaoyan.util.MyBatisUtil;
import com.cskaoyan.util.WdConstant;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author stone
 * @date 2023/03/16 16:39
 */
@WebListener
public class MyBatisInitializationListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        ServletContext servletContext = servletContextEvent.getServletContext();
        SqlSessionFactory sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();
        servletContext.setAttribute(WdConstant.SQL_SESSION_FACTORY, sqlSessionFactory);

        Map<String, Object> data = new ConcurrentHashMap<>();
        data.put("adminService", new AdminServiceImpl());
        servletContext.setAttribute("applicationData", data);

    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}
